package com.drought.rained.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.drought.rained.R;
import com.drought.rained.business.DialogManagerLJ;
import com.drought.rained.utils.ToastUtilsLJ;

public class ShootingDialogLJ {
    private AlertDialog dialog;
    private CallBack callBack;

    @SuppressLint("NewApi")
    public ShootingDialogLJ(final Context context, String title) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.layout_shopting_dialog, null);

        TextView tvTitle = view.findViewById(R.id.tv_title);
        final CheckBox checkAgreement = view.findViewById(R.id.check_agreement);
        TextView tvAgreement = view.findViewById(R.id.tv_agreement);
        TextView confirm = view.findViewById(R.id.button_confirm);

        tvTitle.setText(title);

        builder.setView(view);
        dialog = builder.create();
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_transparent_bg_lj);
        // 不允许取消
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        tvAgreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAgreement.setChecked(true);
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkAgreement.isChecked()) {
                    ToastUtilsLJ.showShort((Activity) context, R.string.privacy_agreement_not_read);
                    return;
                }
                if (callBack != null)
                    callBack.onConfirm();
            }
        });

    }

    public void show() {
        DialogManagerLJ.getManager().showDialogStack(dialog);
    }

    public void hide() {
        DialogManagerLJ.getManager().dismissDialog(dialog);
    }

    public void setOnClickListener(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onConfirm();
    }

}
